package com.yyy.entity;

import com.yyy.entity.Message;

import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author yyy
 * @Date 2020-09-19 23:11
 * @Email yangyouyuhd@163.com
 */
public class QueueWrapper implements Queue<Message> {

    /**
     * 金屋藏队列
     */
    private Queue<Message> queue;

    /**
     * 当前队列是否处于空闲状态
     */
    private AtomicBoolean atomicBoolean = new AtomicBoolean(true);

    public QueueWrapper() {
        this.queue = new LinkedBlockingQueue<>();
    }

    public boolean isIdle() {
        boolean isIdle = atomicBoolean.get();
        return isIdle;
    }

    /**
     * 休息
     */
    public void doIdle() {
        atomicBoolean.set(true);
    }

    /**
     * 试图进入工作状态
     *  如果当前是空闲状态、我成功的将之改为了非空闲
     * @return
     */
    public boolean tryWork() {
        return atomicBoolean.compareAndSet(true,false);
    }
    @Override
    public int size() {
        return queue.size();
    }

    @Override
    public boolean isEmpty() {
        return queue.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return queue.contains(o);
    }

    @Override
    public Iterator<Message> iterator() {
        return queue.iterator();
    }

    @Override
    public Object[] toArray() {
        return queue.toArray(new Message[0]);
    }

    @Override
    public <T> T[] toArray(T[] ts) {
        return queue.toArray(ts);
    }

    @Override
    public boolean add(Message message) {
        return queue.offer(message);
    }

    @Override
    public boolean remove(Object o) {
        return queue.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> collection) {
        return queue.containsAll(collection);
    }

    @Override
    public boolean addAll(Collection<? extends Message> collection) {
        return queue.addAll(collection);
    }

    @Override
    public boolean removeAll(Collection<?> collection) {
        return queue.removeAll(collection);
    }

    @Override
    public boolean retainAll(Collection<?> collection) {
        return queue.retainAll(collection);
    }

    @Override
    public void clear() {
        queue.clear();
    }

    @Override
    public boolean offer(Message message) {
        return queue.offer(message);
    }

    @Override
    public Message remove() {
        return queue.remove();
    }

    @Override
    public Message poll() {
        return queue.poll();
    }

    @Override
    public Message element() {
        return queue.element();
    }

    @Override
    public Message peek() {
        return queue.peek();
    }


/*    ReentrantLock lock = new ReentrantLock();
    Condition notEmpty = lock.newCondition();
    Condition notFull = lock.newCondition();
    public void autoPush() {
        while (true) {
            Message message = take();
            new Method().invoke(null, message);
        }
    }
    public Message take() {
        Message msg = null;
        lock.lock();
        try {
            while (queue.isEmpty()) {
                notEmpty.await();
            }
            msg = queue.poll();
            notFull.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return msg;
    }*/
}
